package com.mango.avtiveMQ;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.io.IOException;

/**
 * @Author:  MangoCookieMilk
 * @Date:     2021/2/28 17:27
 * topic持久化消费者
 */
public class ActiveMQConsumerTopicPersist {
    public static final String ACTIVE_URL="tcp://101.201.249.141:61616";
    public static final String TOPIC_NAME="topic-1";
    public static void main(String[] args) throws JMSException, IOException {
        System.out.println("z3");
        ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory(ACTIVE_URL);
        //通过连接工厂获取connection连接 并启动访问
        Connection connection = activeMQConnectionFactory.createConnection();
        connection.setClientID("z3");
        //创建会话session  需要两个参数，第一个事务，第二个签收
        Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
        //创建目的地(选择是队列还是主题)
        Topic topic = session.createTopic(TOPIC_NAME);
        //订阅者订阅了那个主题
        TopicSubscriber topicSubscriber = session.createDurableSubscriber(topic,"remark.....");
        connection.start();
        //监听主题
        Message message = topicSubscriber.receive();
        while (null != message){
            TextMessage textMessage = (TextMessage) message;
            System.out.println("收到的持久化topic"+textMessage.getText());
            message = topicSubscriber.receive(5000L);
        }
        session.close();
        connection.close();
    }
}
